/**
 * 
 */
package stationGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;


/**
 * @author Owner
 *
 */
public class ProgressScreen extends JPanel  implements ActionListener,PropertyChangeListener {

  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private JProgressBar progressBar;
  private JButton startButton;
  private JTextArea taskOutput;
  private Task task;
  /**
   * 
   */
  public ProgressScreen() {
    super(new BorderLayout());
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.
        createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,1),
        "Progress To Goal:"),BorderFactory.createEmptyBorder(0,5,5,5)));

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);
    progressBar.setStringPainted(true);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5,5,5,5));
    taskOutput.setEditable(false);

    JPanel panel = new JPanel();
    panel.add(startButton);
    panel.add(progressBar);

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    //setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }

  
  
  /**
   * Invoked when the user presses the start button.
   */
  public void actionPerformed(ActionEvent evt) {
      startButton.setEnabled(false);
      //setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      //Instances of javax.swing.SwingWorker are not reusuable, so
      //we create new instances as needed.
      task = new Task();
      task.addPropertyChangeListener(this);
      task.execute();
  }

  /**
   * Invoked when task's progress property changes.
   */
  public void propertyChange(PropertyChangeEvent evt) {
      if ("progress" == evt.getPropertyName()) {
          int progress = (Integer) evt.getNewValue();
          progressBar.setValue(progress);
          taskOutput.append(String.format(
                  "Completed %d%% of task.\n", task.getProgress()));
      } 
  }
  
  
  /**
   * @param arg0
   */
  public ProgressScreen(LayoutManager arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param arg0
   */
  public ProgressScreen(boolean arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param arg0
   * @param arg1
   */
  public ProgressScreen(LayoutManager arg0, boolean arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
  }











    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            Random random = new Random();
            int progress = 0;
            //Initialize progress property.
            setProgress(0);
            while (progress < 100) {
                //Sleep for up to one second.
                try {
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException ignore) {}
                //Make random progress.
                progress += random.nextInt(10);
                setProgress(Math.min(progress, 100));
            }
            return null;
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            startButton.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            taskOutput.append("Done!\n");
        }
    }


}


